﻿using NetTopologySuite.Geometries;
using NetTopologySuite.IO;
using NUnit.Framework;

namespace NetTopologySuite.Samples.Tests.Various
{
    [TestFixture]
    public class StackExchangeFixtures
    {
        /// <summary>
        /// http://gis.stackexchange.com/questions/60303/intersection-between-two-linestring-gives-non-noded-intersection
        /// </summary>
        [Test]
        public void intersection_between_linestrings_gives_topology_error()
        {
            const string ls1Wkt = @"LINESTRING (51055.227410858584 52102.041513978584, 51073.39084032347 52096.46568847251, 51183.32738708461 52062.71727093575, 51280.83632421188 52032.78389190314, 51415.62809024075 51991.40539735807, 51575.27507553736 51942.39682580468, 51669.91610274913 51913.34384027303, 51745.43773052417 51890.16014474777, 51765.32190593836 51884.05608314112, 51855.59683603979 51952.36411358454, 51916.20234851714 51998.2222846924, 52004.718294372215 52065.199350389405, 52004.718294372215 52065.199350389405, 52033.42616870359 52086.921641966816, 52046.98266491563 52097.17939076726, 52057.349397313075 52105.02355161466, 52075.69053924701 52118.901682344665, 52119.54979169772 52152.08851669904, 52122.739555512315 52154.502104652085, 52133.10628790976 52162.34626549949, 52137.89093363166 52165.96664742906, 52137.89093363166 52165.96664742906, 52143.4730203072 52170.19042634689, 52227.20432044038 52233.54711011433, 52300.56888817612 52289.05963303437, 52392.2745978458 52358.45028668442, 52402.64133024324 52366.294447531815, 52415.40038550163 52375.948799344, 52428.95688171367 52386.20654814444, 52645.86082110629 52550.33052895152, 52739.95885363691 52621.53137356636, 52805.34901183616 52671.00992660379)";
            const string ls2Wkt = @"LINESTRING (51313.564671633 52022.73691510973, 51331.728101097884 52017.16108960366, 51441.66464785903 51983.412672066894, 51539.173584986296 51953.47929303429, 51673.96535101517 51912.10079848921, 51833.61233631178 51863.09222693583, 51928.253363523545 51834.03924140418, 52003.77499129859 51810.85554587893, 52131.874967524614 51771.53130283609, 52204.52868538415 51749.228000811796, 52310.64135225795 51716.653441276314, 52345.05627124405 51706.08871926481, 52361.30776076527 51701.09982275937, 52373.73537039913 51697.284784255215, 52391.03842688936 51691.973076799426, 52394.74473141134 51690.83531218692, 52397.82849878731 51693.16869616807, 52441.687751238016 51726.35553052244, 52444.87751505261 51728.76911847549, 52455.24424745006 51736.61327932289, 52460.028893171955 51740.23366125246, 52465.6109798475 51744.45744017029, 52521.20645015943 51786.52467937298, 52522.57757869049 51819.852573264536, 52526.35931204718 51911.77481482129, 52531.08647874304 52026.677616767236, 52531.62085410866 52039.666629161125, 52532.27854686634 52055.65310595361, 52532.97734542138 52072.63873754562, 52544.158122302026 52344.40884301777, 52549.00860638995 52462.30910936231, 52552.37928177309 52544.23980292377)";

            var factory = GeometryFactory.Default;
            var reader = new WKTReader(factory);
            var ls1 = reader.Read(ls1Wkt);
            Assert.That(ls1, Is.Not.Null);
            Assert.That(ls1.IsValid, Is.True);
            Assert.That(ls1, Is.InstanceOf<LineString>());

            var ls2 = reader.Read(ls2Wkt);
            Assert.That(ls2, Is.Not.Null);
            Assert.That(ls2.IsValid, Is.True);
            Assert.That(ls2, Is.InstanceOf<LineString>());

            var intersection = ls1.Intersection(ls2);
            Assert.That(intersection, Is.Not.Null);
            Assert.That(intersection.IsValid, Is.True);
        }
    }
}
